package com.btl.cygf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.btl.view.AutoSearch;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

public class SousuoActivity extends Activity implements OnClickListener {

	private AutoSearch search_auto;

	private AsyncHttpClient client;

	private List<String> auto_list = new ArrayList<>();

	private ArrayAdapter<String> adapter;

	private ImageView search_button;
	private TextView delete_history;

	private ListView history_search_list;

	private SharedPreferences share;
	private Editor editor;
	private SimpleAdapter sa;
	private List<Map<String, Object>> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.sousuo);

		client = new AsyncHttpClient();
		share = getSharedPreferences("history", Activity.MODE_PRIVATE);
		editor = share.edit();
		search_auto = (AutoSearch) findViewById(R.id.search_auto);

		search_button = (ImageView) findViewById(R.id.search_button);
		history_search_list = (ListView) findViewById(R.id.history_search_list);
		delete_history = (TextView) findViewById(R.id.delete_history);
		delete_history.setOnClickListener(this);
		search_button.setOnClickListener(this);

		initView();

		auto_list.clear();

		client.post(getResources().getString(R.string.url)
				+ "/CYGF/queryList.search", new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONArray response) {

				for (int i = 0; i < response.length(); i++) {
					JSONObject object = response.optJSONObject(i);

					auto_list.add(object.optString("list"));

				}

			}

		});

		search_auto.setThreshold(1);

		adapter = new ArrayAdapter<>(getApplicationContext(),
				R.layout.search_item, R.id.search_list, auto_list);

		search_auto.setAdapter(adapter);
		history_search_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent it = new Intent(SousuoActivity.this, SouSuoResult.class);
				it.putExtra("condition", list.get(position).get("content").toString());
				startActivity(it);
				
			}
		});

	}

	private void initView() {
		list = new ArrayList<>();
		String history = share.getString("history", "");
		System.out.println(history);
		String[] strs = history.split(",");
		for (String str : strs) {
			Map<String, Object> map = new HashMap<String, Object>();
			if (!"".equals(str)) {
				map.put("icon", R.drawable.lishi);
				map.put("content", str);
				list.add(map);
			}

		}
		if (list.size() == 0) {
			delete_history.setVisibility(View.GONE);
		} else {
			delete_history.setVisibility(View.VISIBLE);
		}
		

		sa = new SimpleAdapter(getApplicationContext(), list,
				R.layout.search_item2, new String[] { "icon", "content" },
				new int[] { R.id.search_button_list, R.id.search_list2 });
		history_search_list.setAdapter(sa);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.search_button:

			StringBuilder sb = new StringBuilder();

			String history = share.getString("history", "");
			String[] strs = history.split(",");
			sb.append(search_auto.getText().toString());
			for (String str : strs) {

				if (!search_auto.getText().toString().equals(str)) {
					sb.append("," + str);
				}
			}

			editor.putString("history", sb.toString());
			editor.commit();
			
			Intent it = new Intent(SousuoActivity.this, SouSuoResult.class);
			it.putExtra("condition", search_auto.getText().toString());
			startActivity(it);
			
			break;

		case R.id.delete_history:
			editor.putString("history", "");
			editor.commit();
			list.clear();

			sa.notifyDataSetChanged();
			delete_history.setVisibility(View.GONE);
			break;

		default:
			break;
		}

	}

}
